package com.spark.tags;

import com.spark.core.SparkContext;
import com.spark.core.XMap;


public class FieldSet extends Box {
	
	protected static final String PROPERTITY_TITLE= "title";
	private static final String DEFAULT_BODY_CLASS = "form_body";
	
	protected void buildHead(StringBuilder sb, SparkContext context,XMap view, int rows ,int columns) throws Exception{
		String title = view.getString(PROPERTITY_TITLE, "");
		sb.append("<LEGEND class='field_head' unselectable='on'><SPAN>");
		sb.append(title);
		sb.append("</SPAN></LEGEND>");
		
	}
	
	protected void afterBuildTop(StringBuilder sb, SparkContext context,XMap view) throws Exception{
		sb.append("<tbody class='").append(DEFAULT_BODY_CLASS).append("'>");
		super.afterBuildTop(sb, context, view);
	}
	
	protected void buildTop(StringBuilder sb, SparkContext context,XMap view, int rows, int columns, String id) throws Exception{
		
		String cls = view.getString(PROPERTITY_CLASSNAME, "");
		String style = view.getString(PROPERTITY_STYLE, "");
		
		String widthStr = view.getString(PROPERTITY_WIDTH, "0");
//		String wstr = uncertain.composite.TextParser.parse(widthStr, model);
		int width = Integer.valueOf(widthStr).intValue();
		String heightStr = view.getString(PROPERTITY_HEIGHT, "0");
//		String hstr = uncertain.composite.TextParser.parse(heightStr, model);
		int height = Integer.valueOf(heightStr).intValue();
		
		int cellspacing = view.getInt(PROPERTITY_CELLSPACING, 0);
		int cellpadding = view.getInt(PROPERTITY_CELLPADDING, 0);
		
		String className = DEFAULT_TABLE_CLASS;
		String title = view.getString(PROPERTITY_TITLE, "");
		if(!"".equals(title)) className += " " + TITLE_CLASS;
		className += " " + cls;
		
		sb.append("<FIELDSET class='item-fieldset' id='").append(id).append("'");
		StringBuffer sbf = new StringBuffer();
		if(!"".equals(style)) {
			sbf.append(style);
		}
		if(width != 0) sbf.append("width:" + (width-2) + "px;");
		if(height != 0) sbf.append("height:" + height + "px;");
		if(sbf.length() !=0)
		sb.append(" style='").append(sbf).append("'");
		
		sb.append(">");
		buildHead(sb, context, view, rows, columns);
		sb.append("<table width='100%' border=0");
		sb.append(" cellpadding=").append(cellpadding).append(" cellspacing=").append(cellspacing).append(">");
		afterBuildTop(sb,context,view);
	}
	
	protected void buildBottom(StringBuilder sb, SparkContext context,XMap view) throws Exception{
		buildFoot(sb,context,view);
		String showmargin = view.getString(PROPERTITY_SHOW_MARGIN, "true");
		if("true".equals(showmargin))sb.append("<tr height='5'></tr>");
		sb.append("</tbody>");
		sb.append("</table>");
		sb.append("</FIELDSET>");	
	}
}
